package inf102.java.part2;

import inf102.java.part2.generatedFiles.ExerciseMessage.Exercise;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Writer {
	
	private int idCounter = 0;
	
	public static void main(String [] args) throws Exception {
		Writer writer = new Writer();
		writer.run();
	}
	
	private void run() throws Exception {
		
		Scanner scanner = new Scanner(System.in);
		System.out.println("--------------------------Enter A File-------------------------");
		System.out.println("Enter the full path of the file you would like to write to: ");
		File file = new File(scanner.nextLine());
		
		while(true) {
			System.out.println("--------------------Add Or Remove An Exercise------------------");
			System.out.println("a - Add a new exercise");
			System.out.println("b - Remove an exercise");
			System.out.println("q - Quit");
			System.out.println("Enter a command (Select the command's corresponding letter):");
			
			String command = scanner.nextLine();
			
			if (command.equals("a"))
				addExercise(scanner, file);
			else if (command.equals("b"))
				removeExercise(scanner, file);
			else if (command.equals("q"))
				break;
			else
				System.out.println("You have entered an invalid command.");
		}
		
		System.out.println("You have successfully quit the program.");
	}

	private void removeExercise(Scanner scanner, File file) {
		System.out.println("-----------------------Remove an Exercise----------------------");
		System.out.println("Enter the ID of the exercise to remove: ");
		
		int id = scanner.nextInt();
		
	}

	private void addExercise(Scanner scanner, File file) throws Exception{
		System.out.println("-----------------------Select An Exercise----------------------");
		System.out.println("c - Cardio");
		System.out.println("d - Weight Training");
		System.out.println("e - Stretching");
		System.out.println("To input an exercise, type the exercise's corresponding letter: ");
		
		String exercise = scanner.nextLine();
		if (exercise.equals("c")) {
			runCardioOptions(scanner, file);
		}
		else if (exercise.equals("d")) {
			runWeightTrainingOptions(scanner, file);
		}
		else if (exercise.equals("e")) {
			runStretchingOptions(scanner, file);
		}
		else {
			System.out.println("You have selected an invalid key.");
		}
	}

	private void runCardioOptions(Scanner scanner, File file) throws Exception {
		System.out.println("-----------------------Cardio Information----------------------");
		//System.out.println("Enter a unique ID number for this exercise: ");
		//int ID = scanner.nextInt();
		//scanner.nextLine();
		
		System.out.println("Enter a description:");
		String description = scanner.nextLine();
		
		System.out.println("Enter intensity (Scale of 0-10, 10 being the most intense):");
		int intensity = scanner.nextInt();
		scanner.nextLine();
		
		System.out.println("Enter the duration in seconds:");
		int duration = scanner.nextInt();
		scanner.nextLine();
		
		Exercise cardioExercise = Exercise.newBuilder()
				.setId(idCounter)
				.setDescription(description)
				.setIntensity(intensity)
				.setDuration(duration)
				.build();
		
		idCounter++;
		
		writeToFile(file, cardioExercise);
		
		System.out.println("---------------------------------------------------------------");
		System.out.println("----------------------Cardio Exercise Added--------------------");
		System.out.println("---------------------------------------------------------------");
		System.out.println("");
	}
	
	private void runWeightTrainingOptions(Scanner scanner, File file) throws Exception {
		System.out.println("------------------Weight Training Information------------------");
		
		System.out.println("Enter a description:");
		String description = scanner.nextLine();
		
		System.out.println("Enter weight amount:");
		int weightAmount = scanner.nextInt();
		scanner.nextLine();
		
		System.out.println("Enter number of sets:");
		int sets = scanner.nextInt();
		scanner.nextLine();
		
		System.out.println("Enter number of repetitions:");
		int repetitions = scanner.nextInt();
		scanner.nextLine();
		
		Exercise weightTrainingExercise = Exercise.newBuilder()
				.setId(idCounter)
				.setDescription(description)
				.setWeightAmount(weightAmount)
				.setWeightSets(sets)
				.setWeightRepetitions(repetitions)
				.build();
		
		idCounter++;
		
		writeToFile(file, weightTrainingExercise);
		
		System.out.println("---------------------------------------------------------------");
		System.out.println("-----------------Weight Training Exercise Added----------------");
		System.out.println("---------------------------------------------------------------");
		System.out.println("");
	}
	
	private void runStretchingOptions(Scanner scanner, File file) throws Exception {
		System.out.println("--------------------Stretching Information---------------------");
		
		System.out.println("Enter a description:");
		String description = scanner.nextLine();
		
		System.out.println("Enter number of repetitions:");
		int repetitions = scanner.nextInt();
		scanner.nextLine();
		
		System.out.println("Enter the hold duration:");
		int holdDuration = scanner.nextInt();
		scanner.nextLine();
		
		Exercise stretchingExercise = Exercise.newBuilder()
				.setId(idCounter)
				.setDescription(description)
				.setStretchingRepetitions(repetitions)
				.setHoldDuration(holdDuration)
				.build();
		
		idCounter++;
		
		writeToFile(file, stretchingExercise);
		
		System.out.println("---------------------------------------------------------------");
		System.out.println("-------------------Stretching Exercise Added-------------------");
		System.out.println("---------------------------------------------------------------");
		System.out.println("");
	}
	
	private void writeToFile(File file, Exercise exercise)
			throws FileNotFoundException, IOException {
		
		FileOutputStream outputFile = new FileOutputStream(file, true);
		exercise.writeTo(outputFile);
		outputFile.close();
		
		FileInputStream inputFile = new FileInputStream(file);
		Exercise p = Exercise.parseFrom(inputFile);
		inputFile.close();
		
		System.out.println("My ID is: " + p.getId());
		System.out.println("My Description is: " + p.getDescription());
	}
}


